﻿using AspNetCoreWebApiRoute.Module;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace AspNetCoreWebApiRoute.Filter
{
    public class ResultFilter : IActionFilter
    {
        public ILogger<ResultFilter> Logger { get; set; }

        public ResultFilter()
        {

        }

        //public ResultFilter(ILogger<ResultFilter> logger)
        //{
        //    this.Logger = logger;
        //}

        public void OnActionExecuted(ActionExecutedContext context)
        {
            
            var data = context.Result is ObjectResult;
            if (data) {
                ObjectResult result = context.Result as ObjectResult;
                Result result1 = new Result();
                result1.message = "成功";
                result1.code = "200";
                result1.data = result?.Value;
                context.Result = new ObjectResult(result1);
            }
            Console.WriteLine("结果之后");
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var data = context.Result;
            Console.WriteLine("结果之前");
        }
    }
}
